package com.twetec.cms.adapter.pc.backend.tag;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.twetec.cms.adapter.pc.backend.tag.annotation.TplTag;
import com.twetec.cms.adapter.pc.backend.tag.doc.TagDocObject;
import com.twetec.cms.common.constant.ArticleEnum;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.model.po.website.*;
import com.twetec.cms.service.website.ArticleExtendService;
import com.twetec.cms.service.website.ArticleService;
import com.twetec.cms.service.website.ColumnService;
import com.twetec.cms.service.website.SiteService;
import org.beetl.core.tag.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service()
@Scope("prototype")
@TplTag("pagination")
public class PaginationTag extends GeneralVarTagBinding {

	@Autowired
	ColumnService columnService;

	@Override
	public void render() {
		System.out.println("Pagination");

		// 接收标签参数
		Map result = Maps.newHashMap();

		// 当前页
		Integer page = (this.getAttributeValue("page") instanceof String) ?
				Integer.parseInt((String) this.getAttributeValue("page")) : (Integer) this.getAttributeValue("page");

		// 页面总数
		Integer pageCount = (this.getAttributeValue("pageCount") instanceof String) ?
				Integer.parseInt((String) this.getAttributeValue("pageCount")) : (Integer) this.getAttributeValue("pageCount");

		// 不存在
		if (page > pageCount) return;

		// 页面地址
		String pageUrl =(String) this.getAttributeValue("pageUrl");

		ArrayList<Map> pageMapList = new ArrayList<>();

		String lastPage = "";
		String nextPage = "";

//		if (null == pageType || !pageType.equals("checkPage")) {
			// 上一页， 如果当前页为1，或当前页大于1
			Integer lastPageIndex = page.equals(1) ? 1 : page - 1;
			lastPage = pageUrl + "/index_" + lastPageIndex + ".html";

			// 下一页，如果是最后一页，或小于最后一页
			Integer nextPageIndex = page.equals(pageCount) ? pageCount : page + 1;
			nextPage = pageUrl + "/index_" + nextPageIndex + ".html";

			// 只有一页
			if (pageCount.equals(1)) {
				lastPage = pageUrl + "/index.html";
				nextPage = pageUrl + "/index.html";
				HashMap<String, String> pageMap = new HashMap<>();
				pageMap.put("index", "1");
				pageMap.put("url", nextPage);
				pageMapList.add(pageMap);

				result.put("pageList", pageMapList);
				result.put("lastPage", lastPage);
				result.put("nextPage", nextPage);

				this.binds(result);
				this.doBodyRender();
				return;
			}

			// 首页
			HashMap<String, String> pageFirstMap = new HashMap<>();

			// 第一页
			pageFirstMap.put("index", "1");
			pageFirstMap.put("url", pageUrl + "/index_" + 1 + ".html");
			pageMapList.add(pageFirstMap);

			// 拼接 ...
			if (page >= 4) {
				HashMap<String, String> pageMap = new HashMap<>();
				pageMap.put("index", "...");
				pageMap.put("url", "#");
				pageMapList.add(pageMap);
			}

			// 计算要显示页吗的头和尾
			int firstIndex = page - 2;
			int finalIndex = page + 2;

			for (int i = firstIndex; i <= finalIndex; i++) {
				if (i < 2) continue;
				if (i > pageCount) continue;
				HashMap<String, String> pageMap = new HashMap<>();
				pageMap.put("index", String.valueOf(i));
				pageMap.put("url", pageUrl + "/index_" + i + ".html");
				pageMapList.add(pageMap);
			}

			if (finalIndex < pageCount) {
				HashMap<String, String> pageMap = new HashMap<>();
				pageMap.put("index", "...");
				pageMap.put("url", "#");
				pageMapList.add(pageMap);
			}

			HashMap<String, String> pageLastMap = new HashMap<>();

			// 第一页
			pageLastMap.put("index", String.valueOf(pageCount));
			pageLastMap.put("url", pageUrl + "/index_" + pageCount + ".html");
			pageMapList.add(pageLastMap);
//		} else {
//			// 预览的url拼接
//			String url = "";
//			ColumnPO columnPO = columnService.getById(columnId);
//			if (columnPO.getParentId().equals(0L)) {
//				Integer lastPageIndex = page.equals(1) ? 1 : page - 1;
//				lastPage = serverUrl + "website/column/check-column-page" + "?columnId=" + columnId + "&siteId=" + siteId + "&page=" + lastPageIndex;
//
//				// 下一页，如果是最后一页，或小于最后一页
//				Integer nextPageIndex = page.equals(pageCount) ? pageCount : page + 1;
//				nextPage = serverUrl + "website/column/check-column-page" + "?columnId=" + columnId + "&siteId=" + siteId + "&page=" + nextPageIndex;
//
//				url = dm + "website/column/check-column-page" + "?columnId=" + columnId + "&siteId=" + siteId + "&page=";
//			} else if(!columnPO.getParentId().equals(0L)) {
//				Integer lastPageIndex = page.equals(1) ? 1 : page - 1;
//				lastPage = serverUrl + "website/column/check-column-page" + "?columnId=" + columnPO.getParentId() + "&siteId=" + siteId + "&childColumnId=" + columnId + "&page=" + lastPageIndex;
//
//				Integer nextPageIndex = page.equals(pageCount) ? pageCount : page + 1;
//				nextPage = serverUrl + "website/column/check-column-page" + "?columnId=" + columnPO.getParentId() + "&siteId=" + siteId + "&childColumnId=" + columnId + "&page=" + nextPageIndex;
//
//				url = serverUrl + "website/column/check-column-page" + "?columnId=" + columnPO.getParentId() + "&siteId=" + siteId + "&childColumnId=" + columnId + "&page=";
//			}
//
//
//			// 只有一页
//			if (pageCount.equals(1)) {
//				HashMap<String, String> pageMap = new HashMap<>();
//				pageMap.put("index", "1");
//				pageMap.put("url", nextPage);
//				pageMapList.add(pageMap);
//
//				result.put("pageList", pageMapList);
//				result.put("lastPage", lastPage);
//				result.put("nextPage", nextPage);
//
//				this.binds(result);
//				this.doBodyRender();
//				return;
//			}
//
//			// 首页
//			HashMap<String, String> pageFirstMap = new HashMap<>();
//
//			// 第一页
//			pageFirstMap.put("index", "1");
//			pageFirstMap.put("url", url + "1");
//			pageMapList.add(pageFirstMap);
//
//			// 拼接 ...
//			if (page >= 4) {
//				HashMap<String, String> pageMap = new HashMap<>();
//				pageMap.put("index", "...");
//				pageMap.put("url", "#");
//				pageMapList.add(pageMap);
//			}
//
//			// 计算要显示页码的头和尾
//			int firstIndex = page - 2;
//			int finalIndex = page + 2;
//
//			for (int i = firstIndex; i <= finalIndex; i++) {
//				if (i < 2) continue;
//				if (i > pageCount) continue;
//				HashMap<String, String> pageMap = new HashMap<>();
//				pageMap.put("index", String.valueOf(i));
//				pageMap.put("url", url + i);
//				pageMapList.add(pageMap);
//			}
//
//			if (finalIndex < pageCount) {
//				HashMap<String, String> pageMap = new HashMap<>();
//				pageMap.put("index", "...");
//				pageMap.put("url", "#");
//				pageMapList.add(pageMap);
//			}
//
//			HashMap<String, String> pageLastMap = new HashMap<>();
//
//			// 页码末尾
//			pageLastMap.put("index", String.valueOf(pageCount));
//			pageLastMap.put("url", url + pageCount);
//			pageMapList.add(pageLastMap);
//		}

		result.put("pageList", pageMapList);
		result.put("lastPage", lastPage);
		result.put("nextPage", nextPage);

		this.binds(result);
		this.doBodyRender();

	}

	/**
	 * 标签文档
	 * @return
	 */
	public static TagDocObject doc() {
		TagDocObject tagDocObject = new TagDocObject();
		tagDocObject.setTagName("pagination");
		tagDocObject.setDesc("页码标签");
		tagDocObject.setExample("<#pagination serverUrl=\"${serverUrl}\" page=\"${column.page!}\"  pageCount=\"${column.pageCount!}\" pageUrl=\"${column.pageUrl!}\" columnId=\"${column.columnId}\" dm=\"${dm!}\" siteId=\"${site.siteId}\" pageType=\"${pageType!}\" var=\"data\">" +
				"        <a class=\"layui-btn\" href=\"${data.lastPage!}\" >上一页</a>\n" +
				"          @for(page1 in data.pageList){\n" +
				"            <a class=\"layui-btn\" href=\"${page1.url!}\" >${page1.index!}</a>\n" +
				"          @}\n" +
				"        <a class=\"layui-btn\" href=\"${data.nextPage!}\" >下一页</a>\n" +
				"      </#pagination>");

		tagDocObject.setParamDesc("columnId：所属栏目id\n" +
				"serverUrl：资讯发布系统的域名或IP和Port\n" +
				"dm：站点域名\n" +
				"pageType：用于标识是生成静态页面还是预览（预览：checkPage，生成：null或staticPage）\n" +
				"page：页码\n" +
				"pageCount(必填)：总页数\n" +
				"pageUrl：页面路径\n" +
				"--------- var: 标签的数据结果集 ---------\n" +
				"pageList：页码\n" +
				"lastPage：上一页\n" +
				"nextPage：下一页\n");

		tagDocObject.setOtherDesc("无");

		return tagDocObject;
	}

}
